package cn.tedu.egweibo4.controller;

import cn.tedu.egweibo4.commen.response.JsonResult;
import cn.tedu.egweibo4.commen.response.StatusCode;
import cn.tedu.egweibo4.mapper.WeiboMapper;
import cn.tedu.egweibo4.pojo.dto.WeiboDTO;
import cn.tedu.egweibo4.pojo.entity.Weibo;
import cn.tedu.egweibo4.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Api(tags = "微博模块")
@Slf4j
@RestController
@RequestMapping(value = "/v1/weibo")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    /**
     * 发布微博
     */
    @ApiOperation(value = "发布微博")
    @PostMapping(value = "/insert")
    public JsonResult insert(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session){
        //打桩
        log.debug(weiboDTO.toString());
        //校验登录状态
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null)
            return new JsonResult(StatusCode.NOT_LOGIN);
        //发布微博
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO,weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(userVO.getId());
        weiboMapper.insertWeibo(weibo);
        return JsonResult.success();
    }

    /**
     * 微博首页列表
     */
    @ApiOperation(value = "微博首页列表")
    @GetMapping(value = "/selectIndex")
    public JsonResult selectIndex(){
        return JsonResult.success(weiboMapper.selectIndex());
    }

    /**
     * 微博详情页
     */
    @ApiOperation(value = "微博详情页")
    @ApiImplicitParam(name = "id",value = "微博编号",required = true,dataType = "int",example = "200")
    @GetMapping(value = "/selectById")
    public JsonResult selectById(int id){
        return JsonResult.success(weiboMapper.selectById(id));
    }
}
